from web3 import Web3, HTTPProvider

rpc = 'https://bsc-dataseed1.binance.org:443'
web3 = Web3(HTTPProvider(rpc))


def transfer_eth(target_address, amount, gas_price=5, gas_limit=21000):
    nonce = web3.eth.getTransactionCount(address)
    params = {
        'nonce': nonce,
        'to': target_address,
        'value': web3.toWei(amount, 'ether'),
        'gas': gas_limit,
        'gasPrice': web3.toWei(gas_price, 'gwei'),
        'from': address,
    }
    signed_tx = web3.eth.account.signTransaction(params, private_key=private_key)
    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    return tx_hash

def transfer_token(token_contract, target_address, amount, gas_price=5, gas_limit=500000):
    params = {
        "from": address,
        "value": 0,
        'gasPrice': web3.toWei(gas_price, 'gwei'),
        "gas": gas_limit,
        "nonce": web3.eth.getTransactionCount(address),
    }
    func = token_contract.functions.transfer(target_address, web3.toWei(amount, "ether"))
    tx = func.buildTransaction(params)
    signed_tx = web3.eth.account.sign_transaction(tx, private_key=private_key)
    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    return tx_hash